package vip.web3.admin.sys.controller.biz;

import com.alibaba.excel.EasyExcel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import vip.web3.admin.sys.controller.base.AbstractController;
import vip.web3.admin.sys.entity.biz.AUserWalletEntity;
import vip.web3.admin.sys.form.user.UserWalletPageForm;
import vip.web3.admin.sys.form.user.WalletUpdateForm;
import vip.web3.admin.sys.service.biz.IAUserWalletService;
import vip.web3.admin.sys.vo.biz.export.ExWalletVo;
import vip.web3.common.utils.DateUtils;
import vip.web3.common.utils.PageUtils;
import vip.web3.common.utils.R;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户钱包Controller
 * <p>
 * aut-horlilei
 *
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/sys/wallet")
public class AUserWalletController extends AbstractController {

    @Autowired
    private IAUserWalletService aUserWalletService;

    /**
     * 查询用户钱包列表
     */
    @RequiresPermissions("sys:wallet:list")
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params, @Valid UserWalletPageForm form) {
        PageUtils page = aUserWalletService.pageWallets(params, form);
        return R.ok().put("page", page);
    }

    @RequiresPermissions("sys:wallet:list")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam Map<String, Object> params,
                       @Valid UserWalletPageForm form) throws IOException {
        List<ExWalletVo> orders = aUserWalletService.export(params, form);
        if (CollectionUtils.isEmpty(orders)) {
            orders = Collections.EMPTY_LIST;
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("钱包数据_" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExWalletVo.class).sheet("钱包").doWrite(orders);

    }

    /**
     * 新增保存用户钱包
     */
    @RequiresPermissions("sys:wallet:add")
    @PostMapping("/add")
    public R addSave(@Valid @RequestBody AUserWalletEntity aUserWallet) {
        aUserWalletService.save(aUserWallet);
        return R.ok();
    }

    /**
     * 修改用户钱包
     */
    @RequiresPermissions("sys:wallet:edit")
    @GetMapping("/{id}")
    public R edit(@PathVariable("id") Integer id) {
        AUserWalletEntity aUserWallet = aUserWalletService.getById(id);
        return R.ok().put("data", aUserWallet);
    }

    /**
     * 修改保存用户钱包
     */
    @RequiresPermissions("sys:wallet:edit")
    @PostMapping("/edit")
    public  R editSave(@Valid @RequestBody WalletUpdateForm form) {
        aUserWalletService.sysUpdateWallet(form);
        return R.ok();
    }

    /**
     * 删除用户钱包
     */
    @RequiresPermissions("sys:wallet:remove")
    @PostMapping("/remove")
    public R remove(String ids) {
        int i = aUserWalletService.deleteAUserWalletByIds(ids);
        return R.ok();
    }
}
